#!/usr/bin/python
# -*- coding: iso-8859-15 -*-


'''
Created on 11/01/2014

@author: Jesus Gomez
'''

import re

def process_html(text,out):
    
    out.write("{\n")
    for m in re.finditer( '<title>.+</title>', text ):
        #print( "found! title:",text[m.start()+7:m.end()-8] )
        out.write(text[m.start()+7:m.end()-8]+'\n')
    
    for m in re.finditer('<span itemprop="recipeType">.+</span>',text ):
        #print( "found! the type is",text[m.start()+28:m.end()-7] )
        out.write(text[m.start()+28:m.end()-7]+'\n')
        
    for m in re.finditer('<span itemprop="instructions" >.+</span>',text ):
        #print( 'found! ',"the instructions are:",text[m.start()+31:m.end()-7] )
        output = text[m.start()+31:m.end()-7].replace("<br>"," ")+'\n'
        out.write(output)
        
    for m in re.finditer('Dificultad:<br /><strong>.*</strong>', text):
        #print( "found! the dificulty is:",text[m.start()+25:m.end()-9] )
        out.write(text[m.start()+25:m.end()-9]+'\n')
        
    for m in re.finditer("datetime=\".*\">.*</time>",text):
        #print( "found! the time is:",text[m.start():m.end()] )
        #print("found! is vegetarian:" ,text[m.end()+231:m.end()+233])
        
        pos = text[m.start():m.end()].find(">") + 1
        out.write(text[m.start()+pos:m.end()-7]+'\n')
        
        
        out.write(text[m.end()+231:m.end()+233]+'\n')
                          
    for m in re.finditer( "<br />\n<strong>.*</strong>", text):
        #print( "found! is vegetarian:",text[m.end()-11:m.end()-9] )

        out.write(text[m.end()-11:m.end()-9]+'\n')

        
    for m in re.finditer("bold\">.*</span>",text):
        #print( "found! the calories are:",text[m.start()+6:m.end()-14] )
        out.write(text[m.start()+6:m.end()-14]+'\n')
    
    for m in re.finditer("yield\">.*</span>",text):
        #print( "found! the number of eaters:",text[m.start()+7:m.end()-7] )
        out.write(text[m.start()+7:m.end()-7]+'\n')
        
    for m in re.finditer("amount\">.*&nbsp;.*</span>", text):
        #print( "found! the ingredient is:",text[m.start()+7:m.end()] )
        output = text[m.start()+8:m.end()].replace("&nbsp;"," ")
        output = output.replace("<span itemprop=\"name\">", " ")
        output = output.replace("</span>", " ")
        out.write(output+'\n')
    out.write("}\n")
    



# </strong></td> 15
# </tr> 5 + 1
# 1
# <tr> 5 + 1
# <td>Vegetariana:<br /> 23 +1 
# <strong> 9
#         
#     for m in re.finditer("itemprop=\"name\">.*</span>",text):
#         #print( 'found! ',"the ingredient is:",text[m.start()+16:m.end()-7] )